package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.curriculum.questionbank.CategoryType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 试题分类实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：question_categories
 *
 * 管理试题的分类信息，包括分类编码、名称、级别、类型等。
 * 支持试题分类的创建、查询、更新和状态管理。
 * 关联上级分类、科目等关键信息，提供试题分类的完整视图。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("试题分类表")
@SQLDelete(sql = "UPDATE question_categories SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionCategory {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "分类编码不能为空")
    @Size(max = 50, message = "分类编码长度不能超过50个字符")
    @Column(name = "category_code", nullable = false, length = 50, unique = true)
    private String categoryCode;

    @NotBlank(message = "分类名称不能为空")
    @Size(max = 100, message = "分类名称长度不能超过100个字符")
    @Column(name = "category_name", nullable = false, length = 100)
    private String categoryName;

    @Size(max = 500, message = "分类描述长度不能超过500个字符")
    @Column(name = "category_description", length = 500)
    private String categoryDescription;

    @Column(name = "parent_id")
    private Long parentId;

    @Column(name = "parent_code", length = 50)
    private String parentCode;

    @Column(name = "parent_name", length = 100)
    private String parentName;

    @NotNull(message = "分类级别不能为空")
    @Column(name = "category_level", nullable = false)
    private Integer categoryLevel;

    @NotNull(message = "分类类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "category_type", nullable = false, length = 30)
    private CategoryType categoryType;

    @Column(name = "subject_id")
    private Long subjectId;

    @Column(name = "subject_name", length = 100)
    private String subjectName;

    @Column(name = "display_order")
    @Builder.Default
    private Integer displayOrder = 0;

    @Column(name = "icon_name", length = 50)
    private String iconName;

    @Column(name = "color_code", length = 10)
    private String colorCode;

    @Column(name = "is_system_defined")
    @Builder.Default
    private Boolean isSystemDefined = false;

    @Column(name = "is_active")
    @Builder.Default
    private Boolean isActive = true;

    @Column(name = "sort_order")
    @Builder.Default
    private Integer sortOrder = 0;

    @Column(name = "full_path", length = 500)
    private String fullPath;

    @Column(name = "full_path_code", length = 500)
    private String fullPathCode;

    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    // 关联关系
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id", insertable = false, updatable = false)
    private QuestionCategory parent;

    @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<QuestionCategory> children;

}